package com.ruyuan.event.streaming.datagen;
import com.ruyuan.event.streaming.pojo.EventClientLog;
import com.ruyuan.event.streaming.pojo.EventServerLog;
import com.ruyuan.event.streaming.process.ProcessETLUtils;
import com.ruyuan.event.streaming.utils.Constants;
import com.ruyuan.event.streaming.utils.KafkaProducerUtils;
import org.apache.kafka.clients.producer.Producer;

/**
 *  生成serverLog和ClientLog
 */
public class MockEventDataTwo {
    static Producer producer = KafkaProducerUtils.getProducer();

    public static void main(String[] args) throws Exception {
        while (true) {
            mockData();
            Thread.sleep(1000);
        }
    }

    private static void mockData() throws Exception {
        /**
         * 1.生成基础属性
         *
         * 2.客户端的标识信息
         *
         * 3.userInfo信息
         *
         * 4.活动相关数据
         * */

        //1.生成基础属性
        String requestId = MockDataUtils.requestId();
        //timestamp 取系统时间

        //2.客户端的标识信息
        /**
         *     string imei = 11;
         *     string ip = 12;
         *     string network = 13;
         *     string net_type = 14;
         *     string os_name = 15;
         *     string os_version = 16;
         * */
        String imei = MockDataUtils.imei();
        String ip = MockDataUtils.getRandomIp();
        String network = MockDataUtils.network();
        String netType = MockDataUtils.network();
        String os_name = MockDataUtils.getOsNameType();
        String os_version = MockDataUtils.getOsVersion();

        //3.userInfo信息
        /**
         *     string user_id = 31;
         *     string locale = 32;
         *     uint64 birthyear = 33;
         *     string gender = 34;
         *     uint64 joinedAt = 35;
         *     string location = 36;
         *     string country = 37;
         *     string province = 38;
         *     string city = 39;
         *     string timezone = 40;
         * */

        String userId = MockDataUtils.userId();
        String locale = MockDataUtils.getLocale();
        Long birthYear = MockDataUtils.randomDate("2000-01-01","2015-03-01").getTime();
        String gender = MockDataUtils.gender();
        long joinedAt = MockDataUtils.randomDate("2000-01-01","2015-03-01").getTime();
        String location = MockDataUtils.getRoad();
        String country = MockDataUtils.country();
        String province = MockDataUtils.province();
        String city = MockDataUtils.getCity();
        String timezone = MockDataUtils.getTimeZone();

        //4.活动相关数据
        /**
         * //活动编号
         *     string event_id = 50;
         *     //活动举办者
         *     string event_user_id = 51;
         *     //活动开始时间
         *     uint64 event_start_time = 52;
         *     //活动举办的城市
         *     string event_city = 53;
         *     //活动举办的国家
         *     string event_country = 54;
         *     //活动举办的省份
         *     string event_province = 55;
         *
         *     //活动的参与情况：报名，不报名，报名后参加，报名后不参加，不报名后参加活动
         *     string event_type = 60;
         * */
        String eventId = MockDataUtils.userId();
        String event_user_id = MockDataUtils.userId();
        Long event_start_time = MockDataUtils.randomDate("2010-01-01","2022-03-01").getTime();
        String event_city = MockDataUtils.getCity();
        String event_country = MockDataUtils.country();
        String event_province = MockDataUtils.province();

        //生成serverLog
        /**
         * 参数补充：
         * //基础字段1-15预留出来
         *     string request_id = 1;
         *     //时间戳
         *     uint64 timestamp = 2;
         *
         *     //客户端的标识信息
         *     string imei = 21;
         *     string ip = 22;
         *     string network = 23;
         *
         *     //根据当前用户的标签数据匹配对应的活动信息
         *     string user_id = 31;
         *     string locale = 32;
         *     uint64 birthyear = 33;
         *     string gender = 34;
         *     uint64 joinedAt = 35;
         *     string location = 36;
         *     string country = 37;
         *     string province = 38;
         *     string city = 39;
         *     string timezone = 40;
         *
         *     //活动信息数据
         *     //活动编号
         *     string event_id = 50;
         *     //活动举办者
         *     string event_user_id = 51;
         *     //活动举办时间
         *     uint64 event_start_time = 52;
         *     //活动举办的城市
         *     string event_city = 53;
         *     //活动举办的国家
         *     string event_country = 54;
         *     //活动举办的省份
         *     string event_province = 55;
         *
         *     //活动的参与情况：报名，不报名，报名后参加，报名后不参加，不报名后参加活动
         *     string event_type = 60;
         * */
        dataGenServerLog(requestId,imei,ip,network,userId,locale,birthYear,gender,joinedAt,location,country,province,city,timezone,eventId,event_user_id,event_start_time,event_city,event_country,event_province,"SEND");
        /**
         * 生成clientLog
         *
         * //基础字段1-15预留出来
         *     string request_id = 1;
         *     //时间戳
         *     uint64 timestamp = 2;
         *
         *     //客户端的标识信息
         *     string imei = 11;
         *     string ip = 12;
         *     string network = 13;
         *     string net_type = 14;
         *     string os_name = 16;
         *     string os_version = 17;
         *
         *     //user info
         *     string user_id = 21;
         *
         *     //活动编号
         *     string event_id = 50;
         *     //活动举办者
         *     string event_user_id = 51;
         *     //活动开始时间
         *     uint64 event_start_time = 52;
         *     //活动举办的城市
         *     string event_city = 53;
         *     //活动举办的国家
         *     string event_country = 54;
         *     //活动举办的省份
         *     string event_province = 55;
         *
         *     //活动的参与情况：报名，不报名，报名后参加，报名后不参加，不报名后参加活动
         *     string event_type = 60;
         * */
        dataGenClientLog(requestId,imei,ip,network,netType,os_name,os_version,userId,eventId,event_user_id,event_start_time,event_city,event_country,event_province,getEventType());

    }

    private static void dataGenClientLog(String request_id,String imei, String ip,String network,String netType,String osName,String osVersion,
                                           String userId,String event_id,String event_user_id,Long event_start_time,String event_city,String event_country,String event_province,
                                           String event_type) throws Exception {
        EventClientLog eventClientLog = EventClientLog.newBuilder()
                .setRequestId(request_id)
                .setEventId(event_id)
                .setEventUserId(event_user_id)
                .setImei(imei)
                .setIp(ip)
                .setNetwork(network)
                .setNetType(netType)
                .setOsName(osName)
                .setOsVersion(osVersion)
                .setUserId(userId)
                .setEventStartTime(event_start_time)
                .setEventCity(event_city)
                .setEventCountry(event_country)
                .setEventProvince(event_province)
                .setEventType(event_type)
                .build();
        System.out.println(eventClientLog.toString());
        ProcessETLUtils.sendKafkaTopic(producer, Constants.KAFKA_EVENT_CLIENT_LOG,eventClientLog.toByteArray());
    }

    private static void dataGenServerLog(String request_id, String imei, String ip,String network, String user_id, String locale,
                                         Long birth_year, String gender, Long joinedAt, String location, String country,
                                         String province, String city, String timezone, String event_id, String event_user_id,
                                         Long event_start_time, String event_city, String event_country, String event_province,
                                         String event_type) throws Exception {
        EventServerLog serverLog = EventServerLog.newBuilder()
                .setRequestId(request_id)
                .setEventId(event_id)
                .setEventUserId(event_user_id)
                .setTimestamp(System.currentTimeMillis())
                .setImei(imei)
                .setIp(ip)
                .setNetwork(network)
                .setUserId(user_id)
                .setLocale(locale)
                .setBirthyear(birth_year)
                .setGender(gender)
                .setJoinedAt(joinedAt)
                .setLocation(location)
                .setCountry(country)
                .setProvince(province)
                .setCity(city)
                .setTimezone(timezone)
                .setEventType(event_type)
                .build();
        System.out.println(serverLog.toString());
        ProcessETLUtils.sendKafkaTopic(producer, Constants.KAFKA_EVENT_SERVER_LOG,serverLog.toByteArray());
    }

    /**
     * 生成EventType值
             case "join":
             case "not_join":
             case "invited_join":
             case "not_invited_join":
             case "interest":
             case "not_interest":
     * */

    static String[] eventType = {"JOIN","NOT_JOIN","INVITED_JOIN","NOT_INVITED_JOIN","INTEREST","NOT_INTEREST"};
    public static String getEventType() {
        double random = Math.random() * 22;
        return eventType[(int)(random % eventType.length)];
    }
}
